package cn.easybuy.web.backend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.easybuy.entity.Product_category;
import cn.easybuy.service.product.Product_CategoryService;
import cn.easybuy.service.product.Product_CategoryServiceImpl;
import cn.easybuy.utils.CategoryPage;

public class CategoryListServlet extends HttpServlet {

	public CategoryListServlet() {
		super();
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if(action.equals("pageShow")){
			Product_CategoryService pcs = new Product_CategoryServiceImpl();
			CategoryPage page = new CategoryPage();
			String pageNo = request.getParameter("pageNo");
			//如果页号为空 则给个初始值为1 代表首次初始化页面
			if(pageNo==null){
				pageNo="1";
			}
			//设置当前页面号
			page.setPageNo(Integer.parseInt(pageNo));
			//设置每页条数
			String pageSize = "5";
			page.setPageSize(Integer.parseInt(pageSize));
			//获取总数
			int DataCount = pcs.getCount();
			//设置总数
			page.setDataNum(DataCount);
			//设置当前集合对象
			page.setList(pcs.getCategoryList(Integer.parseInt(pageNo), Integer.parseInt(pageSize)));
			//将分页对象page设置到作用域中
			request.setAttribute("page", page);
			//将全部分类设置到作用域中，使用page.list.parseID == CatagoryList.get(0).ID 来展示父ID
			request.setAttribute("CatagoryList", pcs.getList());
			//转发到分类列表页面按照分页展示
			request.getRequestDispatcher("Member_CategoryList.jsp").forward(request, response);
		}else if(action.equals("del")){
			String id = request.getParameter("id");
			Product_CategoryService pcs = new Product_CategoryServiceImpl();
			int i = pcs.delCategorys(id);
			System.out.println(i);
			if(i==-1){
				out.print("<script>");
				out.print("alert('该分类下存在子分类，无法删除');");
				out.print("window.location.href='CategoryListServlet?action=pageShow';");
				out.print("</script>");
			}else if(i==-2){
				out.print("<script>");
				out.print("alert('该分类下存在商品，无法删除!');");
				out.print("window.location.href='CategoryListServlet?action=pageShow';");
				out.print("</script>");
			}else if(i>0){
				out.print("<script>");
				out.print("alert('删除成功!');");
				out.print("window.location.href='CategoryListServlet?action=pageShow';");
				out.print("</script>");
			}else{
				out.print("<script>");
				out.print("alert('删除失败!');");
				out.print("window.location.href='CategoryListServlet?action=pageShow';");
				out.print("</script>");
			}
		}else if(action.equals("forward")){ //新增分类 控制页面转发 类型集合赋值作用域中 新增分类可以取出分类进行选择
			Product_CategoryService pcs = new Product_CategoryServiceImpl();
			List<List<Product_category>> pcList=pcs.getList();
			String ToPage = request.getParameter("ToPage");
			if(ToPage.equals("ToAdd1")){
				request.getRequestDispatcher("Member_UpdateCategory1.jsp").forward(request, response);
			}else if(ToPage.equals("ToAdd2")){
				request.setAttribute("pcList", pcList);
				request.getRequestDispatcher("Member_UpdateCategory2.jsp").forward(request, response);
			}else if(ToPage.equals("ToAdd3")){
				request.setAttribute("pcList", pcList);
				request.getRequestDispatcher("Member_UpdateCategory3.jsp").forward(request, response);
			}
		}else if(action.equals("update")){
			Product_CategoryService pcs = new Product_CategoryServiceImpl();
			String id = request.getParameter("id");
			Product_category pc =  pcs.getProductCategoryById(id);
			List<List<Product_category>> pcList=pcs.getList();
			request.setAttribute("productCategory", pc);
			if(pc.getType()==1){
				//执行修改一级分类
				request.getRequestDispatcher("Member_UpdateCategory1.jsp").forward(request, response);
			}else if(pc.getType()==2){
				//执行修改二级分类
				//将集合存request作用域中
				request.setAttribute("pcList", pcList);
				request.getRequestDispatcher("Member_UpdateCategory2.jsp").forward(request, response);
			}else if(pc.getType()==3){
				//执行修改三级分类
				//将集合存request作用域中
				request.setAttribute("pcList", pcList);
				request.getRequestDispatcher("Member_UpdateCategory3.jsp").forward(request, response);
			}

		}else if(action.equals("add1")){
			String id = request.getParameter("id");

			//id等于0代表新增，数据库中自增列从1开始
			if(Integer.parseInt(id.trim())==0){
				//获取值(一级分类只需要名称 父分类为0 类型为1)
				String name = request.getParameter("name");
				//封装对象
				Product_category pc=new Product_category();
				pc.setName(name);
				pc.setParentId(0);
				pc.setType(1);
				//创建业务逻辑层对象 调用新增方法
				Product_CategoryService pcs = new Product_CategoryServiceImpl();
				int i = pcs.addCategory(pc);
				if(i>0){
					out.print("<script>");
					out.print("alert('新增成功!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}else{
					out.print("<script>");
					out.print("alert('新增成功!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}
			}else{
				//ID不等于0代表修改
				//获取值
				String name = request.getParameter("name");
				//分装分类对象
				Product_category pc = new Product_category();
				pc.setId(Integer.parseInt(id.trim()));
				pc.setName(name);
				pc.setParentId(0);
				pc.setType(1);
				Product_CategoryService pcs = new Product_CategoryServiceImpl();
				int i = pcs.updateCategory(pc);
				if(i>0){
					out.print("<script>");
					out.print("alert('修改成功!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}else{
					out.print("<script>");
					out.print("alert('修改失败!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}
			}
		}else if(action.equals("add2")){
			String id = request.getParameter("id");

			//id等于0代表新增，数据库中自增列从1开始
			if(Integer.parseInt(id.trim())==0){
				//获取值(二级分类需要名称，父分类ID  类型为2)
				String name = request.getParameter("name");
				//封装对象
				Product_category pc=new Product_category();
				pc.setName(name);
				pc.setParentId(Integer.parseInt(request.getParameter("categoryLevel1Id")));
				pc.setType(2);
				//创建业务逻辑层对象 调用新增方法
				Product_CategoryService pcs = new Product_CategoryServiceImpl();
				int i = pcs.addCategory(pc);
				if(i>0){
					out.print("<script>");
					out.print("alert('新增成功!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}else{
					out.print("<script>");
					out.print("alert('新增成功!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}
			}else{
				//ID不等于0代表修改
				//获取值
				String name = request.getParameter("name");
				//分装分类对象
				Product_category pc = new Product_category();
				pc.setId(Integer.parseInt(id.trim()));
				pc.setName(name);
				pc.setParentId(Integer.parseInt(request.getParameter("categoryLevel1Id")));
				pc.setType(1);
				Product_CategoryService pcs = new Product_CategoryServiceImpl();
				int i = pcs.updateCategory(pc);
				if(i>0){
					out.print("<script>");
					out.print("alert('修改成功!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}else{
					out.print("<script>");
					out.print("alert('修改失败!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}
			}
		}else if(action.equals("add3")){
			String id = request.getParameter("id");

			//id等于0代表新增，数据库中自增列从1开始
			if(Integer.parseInt(id.trim())==0){
				//获取值(二级分类需要名称，父分类ID  类型为3)
				String name = request.getParameter("name");
				//封装对象
				Product_category pc=new Product_category();
				pc.setName(name);
				pc.setParentId(Integer.parseInt(request.getParameter("categoryLevel2Id")));
				pc.setType(3);
				//创建业务逻辑层对象 调用新增方法
				Product_CategoryService pcs = new Product_CategoryServiceImpl();
				int i = pcs.addCategory(pc);
				if(i>0){
					out.print("<script>");
					out.print("alert('新增成功!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}else{
					out.print("<script>");
					out.print("alert('新增成功!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}
			}else{
				//ID不等于0代表修改
				//获取值
				String name = request.getParameter("name");
				//分装分类对象
				Product_category pc = new Product_category();
				pc.setId(Integer.parseInt(id.trim()));
				pc.setName(name);
				pc.setParentId(Integer.parseInt(request.getParameter("categoryLevel2Id")));
				pc.setType(2);
				Product_CategoryService pcs = new Product_CategoryServiceImpl();
				int i = pcs.updateCategory(pc);
				if(i>0){
					out.print("<script>");
					out.print("alert('修改成功!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}else{
					out.print("<script>");
					out.print("alert('修改失败!');");
					out.print("window.location.href='CategoryListServlet?action=pageShow';");
					out.print("</script>");
				}
			}
		}
	}
}
